package com.leetcode.array;

/**
 * Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
 * @author Adiy
 *
 */
public class RemoveDuplicatesSortedArray {

	public static void main(String[] args) {

		int nums[] = {0,1,1,2,5};
		int numslen = removeDuplicates1(nums);
		System.out.println("\n" + numslen);
	}

	public static int removeDuplicates(int[] nums) {
		int len = nums.length;		
		int i=0;
		while(i<len-1){
			if(nums[i]!=nums[i+1]){
				i++;
			}else{

				int j = i ;
				while (j <len - 1) {
					nums[j] = nums[j + 1];
					j++;
				}
				len--;
			}
		}
		for (int j = 0; j < len; j++) {
			System.out.print(nums[j] + ",");
		}

		return len;
	}

	/**
	 * reference
	 * @param nums
	 * @return
	 */
	public static int removeDuplicates1(int[] nums) {
		int num = 1, len = nums.length;
		if (len == 0 || len == 1) {
			return len;
		}
		for (int i = 1; i < nums.length; i++) {
			if (nums[num - 1] != nums[i]) // if not the same,just save the
											// different number in the original
											// nums array
			{
				num++;
				nums[num - 1] = nums[i];
			}
		}
		return num;

	}

}
